package com.ww.springboot.boot.old;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 描述：图形化界面学习
 * 1.画出赛道 编码 马编号 模拟马移动
 *
 *
 * @author wanwei
 * @since 2021-05-24 19:23
 */
public class StudyDemo extends JFrame {

    private static int Y = 0;

    public StudyDemo() {
        add(new composition());
    }

    public static void main(String[] args) {
        JFrame frame = new StudyDemo();
        frame.setTitle("赛马");
        frame.setSize(720, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }


    static class composition extends JPanel {

        public composition() {

            Timer timer = new Timer(10, new BallMoveListener());
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.BLACK);
            for (int i = 0; i < 8; i++) {
                //画赛道
                g.drawLine(100 * i, 0, 100 * i, 500);
                //给赛道编号
                g.drawString(String.valueOf(i + 1), i * 100 + 50, 250);
            }

            //画马
            g.setColor(Color.RED);
            for (int i = 0; i < 8; i++) {
                g.drawOval(100 * i + 50, Y, 10, 10);
            }

            if (Y >= 500) {
                g.drawString("结束", 350, 550);
            }
        }

        class BallMoveListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (Y < 500) {
                    Y += 10;
                }
                //重画
                repaint();
            }
        }
    }
}
